// service/serverTime/serverTime.js
import http from "../../utils/http"
import util from "../../utils/util"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    day:0,
    dayTime:[],
    list: [],
    now: new Date().getTime(),
    busy: 1,
    skill_id: ''
  },
  allSet(){
    let arr = this.data.list.map(value =>{
      if(this.data.busy === 1) {
        if(value.state === 0){
          return value.id
        }
      }else {
        if(value.state === 2){
          return value.id
        }
      }
    })
    this.setTime(arr.join(','),this.data.busy === 1 ?  'busy' : 'recover','all')
  },
  setDay(e){
    this.setData({
      day : e.currentTarget.dataset.day
    })
    this.getInfo()
  },
  timeTap(e){
    let info = e.currentTarget.dataset.info
    if(info.state === 0){
      this.setTime(info.id,'busy')
    }else if(info.state === 2){
      this.setTime(info.id,'recover')
    }
  },
  setTime(ids,type,types){
    http.get('skill/skilltime',{
      type,
      ids,
      skill_id: this.data.skill_id
    }).then(res => {
      this.getInfo()
      if(types === 'all'){
        this.setData({
          busy: this.data.busy ===  1 ? 0 : 1
        })
      }
    })
  },
  getInfo(){
    http.get('skill/skilltime',{
      timetype: this.data.day,
      skill_id: this.data.skill_id
    }).then(res => {
      this.setData({
        list: res.data
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      skill_id: options.skill_id
    })
    wx.setNavigationBarTitle({
      title: options.name || '服务时间',
    })
    this.getInfo()
    this.setData({
      dayTime: [
        {day:'今天',time: util.formatTime(this.data.now,'mm-dd')},
        {day:'明天',time: util.formatTime(this.data.now+86400000,'mm-dd')},
        {day:'后天',time: util.formatTime(this.data.now+172800000,'mm-dd')}
      ]
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})